<template>
  <div class="login">
    <!-- 在element-ui官网找到form组件 -->
    <!-- model属性用于保存表单中的数据，最后提交 -->
    <!-- 表单域 model 字段，在使用 validate、resetFields 方法的情况下，该属性是必填的 -->
    <el-form :model="loginForm" class="container" :rules='loginRules' ref="loginRef">
      <el-form-item>
        <div class="avatar">
          <img src="../assets/avatar.jpg" alt="头像">
        </div>
      </el-form-item>
      <el-form-item prop="username">
        <!-- 产看input组件如何添加图标，prefix-icon属性添加前缀 -->
        <el-input v-model="loginForm.username" prefix-icon="myicon myicon-user"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" prefix-icon="myicon myicon-key" type="password"  @keyup.enter.native="handleLogin"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-btn" @click = "handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// 引入登陆函数
import { login } from '@/api'
export default {
  data () {
    return {
      loginForm: {
        // 定义form表单需要的数据
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleLogin () {
      this.$refs.loginRef.validate(isOK => {
        if (isOK) {
          // 如果成功则发送登陆请求
          login({ username: this.loginForm.username, password: this.loginForm.password })
            .then(res => {
              console.log(res)
              if (res.meta.status === 200) {
                console.log(res)
                this.$message({
                  showClose: true,
                  message: '登陆成功',
                  type: 'success'
                })
                // 存储token（用户密钥）值
                localStorage.setItem('logintoken', res.data.token)
                // 存储用户名称
                localStorage.setItem('username', res.data.username)
                // 跳转到主页
                this.$router.push({ name: 'home' })
              } else {
                this.$message({
                  showClose: true,
                  message: res.meta.msg,
                  type: 'error'
                })
              }
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          alert('登陆失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
